package com.wang.luntan.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Random;

/**
 * 全部是一些静态的常量
 *
 * @author Wangsh
 */
@Log4j2
public class ConstatFinalUtil {
    /* 所有的字符串 */
    public static final String ALLSTR = "abcdefghjklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
    /* 日期的默认格式 */
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    /* 日期时间的默认格式 */
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /* 拆分字符串 */
    public static final String SPLIT_STR = "|-->";
    /* 拆分字符串 */
    public static final String SPLIT_STR_ZHUAN = "\\|-->";
    /* 秒 */
    public static final int SECOND = 1000;
    /* 编码 */
    public static final String CHARSET = "UTF-8";
    /* 请求次数 */
    public static final int REQ_COUNT = 5;
    /*请求服务器超时*/
    public static final int REQ_CONNECT_TIMEOUT = 5 * SECOND;
    /*读取超时*/
    public static final int READ_TIMEOUT = 5 * SECOND;
    /* 网络失败次数 */
    public static int networkCount = 0;
    /* 请求头中忽略的key */
    public static final String REQ_HEADER_IGNORE = "noHeader_";
    public static final String USERNAME = "sha256$6zwQnlnCuUJYUMnWQ4RGTeuGnCw98VBT$3a0659af72e9245f7f574fb61794832f134a4e70c6d4b34a6260221092d8590e";

    /* 配置文件中的json对象 */
    public static JSONObject SYS_JSON = new JSONObject();
    /* 配置文件中的json对象,Config模块 */
    public static JSONObject CONFIG_JSON = new JSONObject();
    /* 配置文件中的json对象,info模块 */
    public static JSONObject INFO_JSON = new JSONObject();

    /* 存储Token,管理员对应的信息 */
    public static Map<String, JSONObject> ADMINS_MAP = new HashMap<String, JSONObject>();
    /* 存放所有线程的容器 */
    public static Map<String, Map<String, Object>> THREAD_MAP = new Hashtable<String, Map<String, Object>>();
    /* 常见的请求头信息 */
    public static JSONArray HTTP_JSONARR = new JSONArray();

    /*
     * 放一些初始化的操作 只执行一次
     */
    static {
        /* FastJSON中默认的格式 */
        JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
        FileUtil fileUtil = new FileUtil();
        /*
         * 从Classpath中获取 classpath在target下面,但是配置的时候让classpath在target下面不显示 workspace里面有,
         * 编译后的class文件的相对路径是忽略包的 把ConstatFinalUtil所在class文件的包忽略掉,就和config.json在同一目录下了
         */
        String result = fileUtil.readFile(ConstatFinalUtil.class.getClassLoader().getResourceAsStream("config.json"));
        /* 将字符串变成JSON对象 */
        try {
            SYS_JSON = (JSONObject) JSON.parse(result);
            CONFIG_JSON = SYS_JSON.getJSONObject("config");
            INFO_JSON = SYS_JSON.getJSONObject("info");
        } catch (Exception e) {
            log.error("程序报错了", e);
        }
    }

    /**
     * 随即休眠
     * st-->ed 百毫秒,间隔是300毫秒
     * type:slow(慢),fast:(快)
     * 单位是毫秒
     */
    public static void randSleep(String type) {
        int st = 0;
        int ed = 0;
        if ("slow".equalsIgnoreCase(type)) {
            /* 慢 */
            st = 10;
            ed = 30;
        } else if ("fast".equalsIgnoreCase(type)) {
            /* 快 */
            st = 3;
            ed = 10;
        }
        Random rand = new Random();
        while (true) {
            int randInt = (rand.nextInt(ed));
            if (randInt >= st) {
                try {
                    Thread.sleep(randInt * 100);
                    log.info("休眠时长(毫秒):{}", randInt * 100);
                    break;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 随机设置Header
     */
    public static void headerRandom(Map<String, String> headerMap) {
        Random random = new Random();
        /* 随机获取 */
        int randInt = random.nextInt(ConstatFinalUtil.HTTP_JSONARR.size());
        /* 获取随机的Cookie */
        JSONObject httpJSON = (JSONObject) ConstatFinalUtil.HTTP_JSONARR.get(randInt);
        /* 获取:userAgent */
        String userAgent = "user-agent";
        if (headerMap.get(userAgent) == null) {
            headerMap.put(userAgent, httpJSON.getString(userAgent));
        }

        /* 获取:cookie */
        String cookie = "cookie";
        if (headerMap.get(cookie) == null) {
            headerMap.put(cookie, httpJSON.getString(cookie));
        }
    }
}
